package com.sl.hx.shop;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.sl.hx.shop.base.BaseActivity;
import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.bean.SplashBean;
import com.sl.hx.shop.constant.SPUKey;
import com.sl.hx.shop.http.ApiFactory;
import com.sl.hx.shop.http.HttpUtil;
import com.sl.hx.shop.http.ProgressSubscribe;
import com.sl.hx.shop.ui.home.activity.SlectStoreActivity;
import com.sl.hx.shop.ui.home.activity.WebAvtivity;
import com.sl.hx.shop.utils.SPUtils;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.umeng.analytics.MobclickAgent;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;

public class SplashActivity extends BaseActivity {


    @BindView(R.id.iv_advertising)
    ImageView ivAdvertising;
    @BindView(R.id.tv_second)
    TextView tvSecond;
    @BindView(R.id.layout_skip)
    LinearLayout layoutSkip;

    private SplashBean splashBean;
    private int recLen = 3;//跳过倒计时提示3秒
    Timer timer = new Timer();
    private Handler handler;
    private Runnable runnable;
    private static boolean isAdClicked = false;//是否点击了广告


    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        //接收参数
        splashBean=getIntent().getParcelableExtra(SPUKey.SPLASHBEAN);

        //隐藏状态栏
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);

        timer.schedule(task, 1000, 1000);//等待时间一秒，停顿时间一秒

        handler = new Handler();
        handler.postDelayed(runnable = new Runnable() {
            @Override
            public void run() {
                //如果点击了广告那么停止启动应用，没点广告的话就按正常情况启动
                if (isAdClicked){
                    return;
                }else {
                    toActivity(MainActivity.class);
                    finish();
                    timer.cancel();
                }
            }
        }, 3000);//延迟3S后发送handler信息

        Glide.with(this).load(splashBean.getLogo()).into(ivAdvertising);
    }


    @Override
    protected void initData() {
    }

    @OnClick({R.id.iv_advertising, R.id.layout_skip})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_advertising:
                splashClick();
                break;
            case R.id.layout_skip:
                //从闪屏界面跳转到首界面
                toActivity(MainActivity.class);
                finish();
                timer.cancel();
                if (runnable != null) {
                    handler.removeCallbacks(runnable);
                }

                break;
        }
    }

    //图片点击事件
    private void splashClick() {
        //点击广告图片设置标记并跳转页面
        isAdClicked = true;
        if (!TextUtils.isEmpty(splashBean.getUrl())){
            //跳h5
            Intent intent = new Intent(this,WebAvtivity.class);
            intent.putExtra("webviewUrl",splashBean.getUrl());
            intent.putExtra("webviewTitle",splashBean.getName());
            startActivity(intent);

        }else if (splashBean.getFavorites_id()>0){
            //跳选品库
            Intent intent = new Intent(this, SlectStoreActivity.class);
            intent.putExtra("slectstore_favorites_id",splashBean.getFavorites_id());
            intent.putExtra("slectStore_name",splashBean.getName());
            startActivity(intent);
        }
        if (runnable != null) {
            handler.removeCallbacks(runnable);
        }
    }

    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() { // UI thread
                @Override
                public void run() {
                    recLen--;
                    tvSecond.setText("" + recLen);
                    if (recLen < 0) {
                        timer.cancel();
                        //tvSecond.setVisibility(View.GONE);//倒计时到0隐藏字体
                        tvSecond.setText(" ");
                    }
                }
            });
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        //从广告页回退时再延迟一点执行启动程序，并改变标记状态
        if (isAdClicked) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    //如果点击了广告那么停止启动应用，没点广告的话就按正常情况启动
                    if (isAdClicked){
                        return;
                    }else {
                        toActivity(MainActivity.class);
                        finish();
                        timer.cancel();
                    }
                }
            }, 800);
        }

        isAdClicked = false;

        //友盟统计
        MobclickAgent.onResume(this);
    }


    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
        handler.removeCallbacks(runnable);
    }
}
